package com.qf.ticketredis.conf;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2 //启用swagger
@EnableKnife4j //启用knife4k
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.OAS_30);

        //定义文档的说明
        docket.apiInfo(new ApiInfo(
                "cdm接口文档",
                "这是cdm编写的关于买票系统的接口文档",
                "1.0",
                "http://www.1000phone.com",
                new Contact(
                        "cdm",
                        "http://www.cdm.com",
                        "cdm@1000phone.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()));

        docket.groupName("ticket文档")
                .select()
                //选择com.qf包下，所有/ticket/相关的接口生成到ticket文档中
                .apis(RequestHandlerSelectors.basePackage("com.qf"))
                .paths(PathSelectors.ant("/**"))
                .build();

        //给当前文档中所有接口设置共性的参数（行中，体中，头中）
        docket.globalRequestParameters(globalReqs());

        return docket;
    }

    private List<RequestParameter> globalReqs() {
        List<RequestParameter> globalReqs = new ArrayList<>();

        RequestParameter requestParameter =
                new RequestParameterBuilder()
                        //这个参数是请求头
                        .in(ParameterType.HEADER)
                        //头的名字
                        .name("token")
                        //对参数的描述
                        .description("登陆后获取到的认证头信息")
                        //这个参数是必须的
                        .required(true)
                        .build();

        globalReqs.add(requestParameter);


        return globalReqs;
    }

//    @Bean
//    public Docket docket2(){
//        Docket docket = new Docket(DocumentationType.OAS_30);
//
//        //定义文档的说明
//        docket.apiInfo(new ApiInfo(
//                "cdm接口文档",
//                "这是cdm编写的关于用户系统的接口文档",
//                "1.0",
//                "http://www.1000phone.com",
//                new Contact(
//                        "cdm",
//                        "http://www.cdm.com",
//                        "cdm@1000phone.com"),
//                "Apache 2.0",
//                "http://www.apache.org/licenses/LICENSE-2.0",
//                new ArrayList<>()));
//
//        docket.groupName("user文档")
//                .select()
//                //选择com.qf包下，所有/user/相关的接口生成到user文档中
//                .apis(RequestHandlerSelectors.basePackage("com.qf"))
//                .paths(PathSelectors.ant("/user/**"))
//                .build();
//
//        return docket;
//    }
}
